package znbkTestPaperBj.activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lancooznbk.R;
import com.example.lancooznbk.view.base.BaseActivity;
import com.example.lancooznbk.view.widget.ClickImageView;
import com.example.lancooznbk.view.widget.LoadingDialog;
import com.example.lancooznbk.view.widget.ScaleButton;

import lancooControl.AndroidBug5497Workaround;
import tcking.github.com.giraffeplayer.GiraffePlayer;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import znbkListSc.entity.CollectionListEntity;
import znbkTestPaperBj.ZnbkPaperBjInfor;
import znbkTestPaperInfor.ZnbkPaperDef;

import static znbkTestPaperBj.activity.NotePaperActivity.mEntityBj;

/**
 * Created by Administrator on 2018\3\23 0023.
 */

public class NoteCheckOriginalQuestionPaperSplActivity extends BaseActivity implements View.OnLayoutChangeListener {

    private ScaleButton mSBtnBack;//返回按钮
    private TextView mTvTittle;
    private ScaleButton mSBtnMore;
    private LinearLayout mLayoutTitleType;
    private TextView mTvTitleType;
    private TextView mTvTypeName;
    private LinearLayout mLayoutBasicInfor;//基本信息：创建时间，推荐人气
    private TextView mTvCreateTime;
    private TextView mTvRecommendNumberOfTimes;
    private LinearLayout mLayoutTools;//工具栏：笔记，知识点
    private ClickImageView mCivNotes;
    private ClickImageView mCivKnowledgePoint;

    GiraffePlayer player;
    private CollectionListEntity.FavouriteBean mEntity;
    public static final  String PAPER_ENTITY = "paperEntity";
    public static final String PAPER_FROM = "paperFrom";
    public int mQuesOrPaperFrom;
    private int ModelFrom;//试卷来源
    /*
   * 获取作文题用户相关信息
   * */
    private String ResName = "";
    private String ResTypeName = "";
    private String CreateTime = "";
    private int RecommendNumberOfTimes = -1;
    public String VideoPath = "";

    private LoadingDialog mLoadingDialog;
    //Activity最外层的Layout视图
//    private View activityRootView;

    public  static void launch(BaseActivity activity,@ZnbkPaperDef.PaperFrom int quesOrPaperFrom){
        Bundle bundle = new Bundle();
        bundle.putInt(PAPER_FROM,quesOrPaperFrom);
        Intent intent = new Intent(activity, NoteCheckOriginalQuestionPaperSplActivity.class);
        intent.putExtras(bundle);
        activity.startActivityForResult(intent,1);
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.activity_paper_spl;
    }

    @Override
    protected void initViews() {

        initView();

        AndroidBug5497Workaround.assistActivity(this);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null){
            mEntity = (CollectionListEntity.FavouriteBean) bundle.getSerializable(PAPER_ENTITY);
            mQuesOrPaperFrom = bundle.getInt(PAPER_FROM);
        }

        //核心代码.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }


        player = new GiraffePlayer(this);
        player.onComplete(new Runnable() {
            @Override
            public void run() {
                //callback when video is finish
                Toast.makeText(getApplicationContext(), "video play completed", Toast.LENGTH_SHORT).show();
            }
        }).onInfo(new GiraffePlayer.OnInfoListener() {
            @Override
            public void onInfo(int what, int extra) {
                switch (what) {
                    case IMediaPlayer.MEDIA_INFO_BUFFERING_START:
                        //do something when buffering start
                        break;
                    case IMediaPlayer.MEDIA_INFO_BUFFERING_END:
                        //do something when buffering end
                        break;
                }
            }
        }).onError(new GiraffePlayer.OnErrorListener() {
            @Override
            public void onError(int what, int extra) {
                Toast.makeText(getApplicationContext(), "video play error",Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    protected void updateViews(boolean isRefresh) {
        mSBtnMore.setVisibility(View.GONE);

                /*
        * 显示资源概况
        * */
        ResName = mEntityBj.getModelTKQue().getResName();
        if (ResName.equals("")) {
            ResName = mEntityBj.getModelTKQue().getResTypeName();
        }

        /*
        * 资源类型
        * */
        ResTypeName = mEntityBj.getModelTKQue().getResTypeName();

//        /*
//        * 资源创建时间
//        * */
//        CreateTime = ZnbkPaperScInfor.CreatTime;
//
//        /*
//        * 推荐人气
//        * */
//        RecommendNumberOfTimes = ZnbkPaperScInfor.RecommendNumberOfTimes;

        /*
        * 视频路径
        * */
        VideoPath = mEntityBj.getModelTKQue().getVedioList().get(0).getAudioPath();
        player.play(VideoPath);
        player.setShowNavIcon(false);

        /*
        * 显示题型名称
        * */
        mTvTypeName.setText(ResTypeName);

//        /*
//        * 隐藏基本信息
//        * */
//        mLayoutBasicInfor.setVisibility(View.GONE);
//        /*
//        * 隐藏工具栏
//        * */
//        mLayoutTools.setVisibility(View.GONE);

    }

    /*
 * 初始化界面控件
 * */
    private void initView(){

        /*
        * 返回按钮菜单相关设置
        * */
        mSBtnBack = (ScaleButton) findViewById(R.id.sbtn_back);
        mSBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HideKeyboard();
                finish();
            }
        });


        /*
        * 页面头部-》收藏详情：mTvTittle
        * */
        mTvTittle = (TextView) findViewById(R.id.tv_title);
        mTvTittle.setText(R.string.znbk_titleNoteOriginalQuestion);

        /*
        * 显示笔记原题栏：mLayoutTitleType
        * 显示笔记原题栏文字： mTvTitleType
        * */
        mLayoutTitleType = (LinearLayout) findViewById(R.id.layout_titleType);
        mLayoutTitleType.setVisibility(View.VISIBLE);
        mTvTitleType  = (TextView) findViewById(R.id.tv_titleType);
        mTvTitleType.setText(R.string.znbk_quesNote);

        /*
        * 更多功能按钮菜单相关信息
        * */
        mSBtnMore = (ScaleButton) findViewById(R.id.sbtn_more);

        mTvTypeName = (TextView) findViewById(R.id.tv_typeName);
        mLayoutBasicInfor = (LinearLayout) findViewById(R.id.layout_basicInfor);
        /*
        * 隐藏基本信息
        * */
        mLayoutBasicInfor.setVisibility(View.GONE);
        mTvCreateTime = (TextView) findViewById(R.id.tv_createTime);
        mTvRecommendNumberOfTimes = (TextView) findViewById(R.id.tv_recommendNumberOfTimer);
        mLayoutTools = (LinearLayout) findViewById(R.id.layout_tools);
        /*
        * 隐藏工具栏
        * */
        mLayoutTools.setVisibility(View.GONE);
        mCivNotes = (ClickImageView) findViewById(R.id.civ_notes);
        mCivKnowledgePoint = (ClickImageView) findViewById(R.id.civ_knowledge_point);

//        activityRootView = findViewById(R.id.main);

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (player != null) {
            player.onPause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (player != null) {
            player.onResume();
        }
        //添加layout大小发生改变监听器
//        activityRootView.addOnLayoutChangeListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (player != null) {
            player.onDestroy();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (player != null) {
            player.onConfigurationChanged(newConfig);
        }
    }

    @Override
    public void onBackPressed() {
        if (player != null && player.onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }

    public  LoadingDialog getLoadingDialog(){
        return mLoadingDialog;
    }

    /***********************************************************************************************************************
     /*
     * LazyViewPage切换事件监听
     * */
    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
//			Log.e("onPageScrolled==111==: ", "==arg0=="+arg0);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
//			Log.e("onPageScrolled==111==: ", "==arg0=="+arg0);
        }

        @Override
        public void onPageSelected(int arg0) {

        }

    }


    //隐藏虚拟键盘:Fragment进行切换的时候使用
    public  void HideKeyboard() {
        /**隐藏软键盘**/
        View viewK = this.getWindow().peekDecorView();
        if(viewK != null){
            InputMethodManager inputmanger = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(viewK.getWindowToken(), 0);
        }
    }

    /*
    * 监听系统home事件
    * */
    @Override
    protected void onUserLeaveHint() {
        super.onUserLeaveHint();
    }

    /*
    * 监听系统返回事件
    * */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            finish();
            return false;
        }
        return false;
    }


    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {

        if (bottom > oldBottom){
            //输入法软键盘关闭
            Log.d(">>>>>>>>>crg", "输入法软键盘关闭: ");
        } else if (bottom < oldBottom){
            //输入法软键盘打开
            Log.d(">>>>>>>>>crg", "输入法软键盘打开: ");
        }

    }

}